/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * OperationLogServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.OperationLogMapper;
import com.yixun.qfbao.model.OperationLog;
import com.yixun.qfbao.model.dto.OperationLogDto;
import com.yixun.qfbao.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * [文件名称]<br>
 * OperationLogServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 用户操作日志表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-08-07 16:20:41 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("operationLogService")
public class OperationLogServiceImpl implements OperationLogService {
	
	@Autowired
	private OperationLogMapper mapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<OperationLogDto> getPageModel(OperationLogDto entity,int begin,int end){
		Integer total = mapper.countByDto(entity);
		List<OperationLogDto> rows = mapper.listByDto(entity,begin,end);
	    Page<OperationLogDto> msr = new Page<OperationLogDto>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<OperationLog> selectListByDto(OperationLogDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public OperationLog selectByDto(OperationLogDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public OperationLog selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public OperationLogDto create(OperationLogDto entity) {
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(OperationLogDto entity) {
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(OperationLogDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(OperationLogDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public OperationLog selectBy(OperationLog entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	@Override
	public List<OperationLog> getOperationsByTkMapper(OperationLogDto entity) {
		List<OperationLog> operationLogs = new ArrayList<>();
		Example example = new Example(OperationLog.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", entity.getId())
				.andEqualTo("optType", "99")
				.andLike("optTarget", entity.getOptTarget());
		if(Objects.nonNull(entity.getOptTimeBefore())){
			criteria.andGreaterThanOrEqualTo("optTime", entity.getOptTimeBefore());
		}
		if(Objects.nonNull(entity.getOptTimeAfter())){
			criteria.andLessThanOrEqualTo("optTime", entity.getOptTimeAfter());
		}
		operationLogs = mapper.selectByExample(example);

		return operationLogs;

	}
}
